package com.shujia

object Demo6Apply {
  def main(args: Array[String]): Unit = {
    val j: J = new J("001", "zhangsan")
    println(j._id)

    val q: Q = Q.apply("002", "lisi")
    println(q._id)
    // apply可以省略 直接通过 ”类对象"名直接进行调用
    // 可以通过apply方法省略new关键字
    val q2: Q = Q("003", "xiaoming")
    println(q2._id)
  }

}

class J(id: String, name: String) {
  val _id: String = id
  val _name: String = name
}

class Q(id: String, name: String) {
  val _id: String = id
  val _name: String = name
}

// Q类的伴生对象
object Q {
  def apply(id: String, name: String): Q = {
    new Q(id, name)
  }
}
